from django.shortcuts import render, HttpResponse
from .models import Publisher, Book


# Create your views here.


def add_publisher(request):
    Publisher.objects.create(name='机械出版社', address="合肥")
    return HttpResponse('add publisher')


def add_book(request):
    # 添加出版社信息 传递id   常用
    Book.objects.create(title='python入门到精通', price=10, publisher_id=2)
    return HttpResponse('add Book')


def select_book(request):
    book = Book.objects.filter(id=3).first()
    print(f'图书的基本基本信息是{book.title},{book.price}')
    # 获取当前图书的出版社信息
    p_name = book.publisher.name
    p_address = book.publisher.address
    print(f'图书的出版社信息是{p_name},{p_address}')
    return HttpResponse(' select  book  ')


def select_publisher(request):
    publisher = Publisher.objects.filter(id=2).first()
    print(f'出版社的基本信息是{publisher.name},{publisher.address}')
    # 获取当前出版社出版的所有图书  book_set
    books = publisher.book_set.all()
    # books = publisher.book_set.filter(title='python入门')
    print('-------------出版社出版的所有图书信息----------------')
    for book in books:
        print(f'{book.title},{book.price}')
    return HttpResponse(' select  publisher  ')


def delete_book(request):
    Book.objects.filter(id=1).delete()
    return HttpResponse(' delete  book ok ')


def delete_publisher(request):
    Publisher.objects.filter(id=2).delete()
    return HttpResponse(' delete  Publisher ok ')
